"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.IsNoEmpty = void 0;
const constant_1 = require("./constant");
function createValidate(fn) {
    return (message) => {
        return (target, key) => {
            const arr = Reflect.getMetadata(constant_1.DTO, target) || [];
            arr.push({
                fn,
                message,
                key
            });
            Reflect.defineMetadata(constant_1.DTO, arr, target);
        };
    };
}
exports.IsNoEmpty = createValidate((val) => {
    return val != null && val !== '';
});
function validate(data, dto) {
    const metadata = Reflect.getMetadata(constant_1.DTO, dto);
    if (Array.isArray(metadata)) {
        const errors = metadata.map(({ fn, message, key }) => {
            const valid = fn(data[key]);
            return {
                message,
                key,
                valid
            };
        }).filter((t) => !t.valid);
        return errors;
    }
    return [];
}
exports.validate = validate;
